package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.IndexImg;
import com.powernode.model.Result;
import com.powernode.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/4/26 20:38
 */
@RestController
@RequestMapping("admin/indexImg")
@Slf4j()
@Api(tags = "轮播图管理")
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;

    /**
     * 查询所有轮播图信息
     * @param current
     * @param size
     * @param status
     * @return
     */
    @ApiOperation("查询所有轮播图")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> loadIndexImgPage(
            @RequestParam(value = "current") Long current,
            @RequestParam(value = "size") Long size,
            @RequestParam(required = false, value = "status") Integer status
    ){

        Page page = new Page<IndexImg>(current, size);

        page = indexImgService.page(page,
                new LambdaQueryWrapper<IndexImg>().eq(ObjectUtils.isNotNull(status), IndexImg::getStatus, status)
        );

        return Result.success(page);
    }

    /**
     * 根据轮播图id查询轮播图
     * @return
     */
    @ApiOperation("根据轮播图id查询轮播图")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public Result<IndexImg> getIndexImgInfoById(@PathVariable(value = "id") Long id){

        IndexImg indexImg = indexImgService.queryIndexImgById(id);


        return Result.success(indexImg);
    }


    /**
     * 修改轮播图
     * @return
     */
    @ApiOperation("修改轮播图")
    @PutMapping()
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<String> updateIndexImg(@RequestBody IndexImg indexImg){

        if (indexImg.getType() == -1){
            indexImg.setProdId(null);
        }
        boolean b = indexImgService.updateById(indexImg);

        return Result.handle(b);
    }

    /**
     * 删除轮播图
     * @return
     */
    @ApiOperation("删除轮播图")
    @DeleteMapping()
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<String> deleteIndexImg(@RequestBody List<Long>ids){

        boolean b = indexImgService.removeBatchByIds(ids);

        return Result.handle(b);
    }

    /**
     * 保存轮播图
     * @return
     */
    @ApiOperation("保存轮播图")
    @PostMapping()
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public Result<String> saveIndexImg(@RequestBody IndexImg indexImg){
        indexImg.setProdId(1L);
        indexImg.setCreateTime(new Date());
        if (indexImg.getType()== -1){
            indexImg.setProdId(null);
        }
        boolean b = indexImgService.save(indexImg);
        return Result.handle(b);
    }

}
